/**
* Copyright (C) 2013-2024 Nanjing Pengyun Network Technology Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ 

package py.datanode.segment.datalog.broadcast;

import py.datanode.segment.datalog.LogImage;
import py.datanode.segment.datalog.MutationLogEntry;
import py.datanode.segment.datalog.MutationLogEntryFactory;
import py.datanode.segment.datalog.broadcast.exception.HeIsBrokenLogException;
import py.datanode.segment.datalog.broadcast.exception.ImBrokenLogException;

/**
 * broken log without log id, the log entry did come but for some reason we couldn't create a real
 * mutation log entry .
 */
public class BrokenLogWithoutLogId extends LogWithoutLogId {
  private BrokenReason reason;

  public BrokenLogWithoutLogId(long logUuid, BrokenReason reason) {
    super(MutationLogEntryFactory.createEmptyLog(logUuid, LogImage.INVALID_LOG_ID, -1, -1));
    this.reason = reason;
  }

  @Override
  public CompleteLog tryComplete(LogWithoutLogId another) throws ImBrokenLogException {
    throw new ImBrokenLogException();
  }

  @Override
  public CompleteLog tryComplete(BrokenLogWithoutLogId another)
      throws ImBrokenLogException, HeIsBrokenLogException {
    throw new ImBrokenLogException();
  }

  @Override
  public CompleteLog tryComplete(LogWithoutLogEntry another) throws ImBrokenLogException {
    throw new ImBrokenLogException();
  }

  @Override
  public MutationLogEntry getLogEntry() {
    return null;
  }

  public boolean isSuccess() {
    switch (reason) {
      case Discarded:
        return true;
      default:
        return false;
    }
  }

  @Override
  public String toString() {
    return "BrokenLogWithoutLogId{" + "super=" + super.toString() + ", reason=" + reason + '}';
  }

  public enum BrokenReason {
    ResourceExhausted, Discarded, BadRequest
  }
}
